﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Collections;
using System.IO;
using Tracy.CoreLib.Data;

namespace Tracy.CoreLib
{
    public static class CommonFunctions
    {
        public static IList AppendCollection(IList original, IList toAppend, bool keepDup)
        {
            foreach (object obj in toAppend)
            {
                if (keepDup || !original.Contains(obj)) original.Add(obj);
            }
            return original;
        }

        public static IXmlModelCollection AppendCollection(IXmlModelCollection original, IXmlModelCollection toAppend)
        {
            foreach (IXmlModelItem item in toAppend)
            {
                if (!original.Contains(item.Key)) original.Add(item);
            }
            return original;
        }

        public static string CollectionToString(IEnumerable collection)
        {
            StringWriter sw = new StringWriter();
            foreach(object o in collection)
            {
                sw.WriteLine(o.ToString());
            }
            return sw.ToString();
        }

        public static bool Equals(ResourceItem a, ResourceItem b)
        {
            return a.Link == b.Link;
        }
    }

    public class SortedByStringComparer : IComparer<string>
    {
        static private SortedByStringComparer mono;
        public static SortedByStringComparer Default
        {
            get
            {
                if (mono == null)
                    mono = new SortedByStringComparer();
                return mono;
            }
        }

        #region IComparer
        public int Compare(string x, string y)
        {
            int result = x.CompareTo(y);
            if (result == 0) return -1;
            else return result;
        }
        #endregion
    }

    public class EventArgsWithData : EventArgs
    {
        private object _data;

        public object Data
        {
            get { return _data; }
            set { _data = value; }
        }
        public EventArgsWithData(object data):base()
        {
            _data = data;
        }
    }
}
